﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for WF
/// </summary>
public sealed class WF
{
    public WF()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    /// <summary>
    /// Starts Workflow of specified type and returns newly created Project Code
    /// </summary>
    /// <param name="WFC">Workflow Type</param>
    /// <param name="CustomerId"></param>
    /// <param name="ClientId"></param>
    /// <param name="ex"></param>
    /// <returns></returns>
    public static string StartWorkflow(ENU.WFCode WFC, int CustomerId, int ClientId, ref ERR.UIException exUI)
    {
        string ProjectCode = null;
        switch (WFC)
        {
            case ENU.WFCode.RP:
                ProjectCode = WFRPStart(ClientId, CustomerId, ref exUI);
                break;
            case ENU.WFCode.WP:
                break;
            case ENU.WFCode.AS:
                break;
            case ENU.WFCode.HL:
                break;
            case ENU.WFCode.HS:
                break;
            case ENU.WFCode.SS:
                break;
            case ENU.WFCode.FS:
                break;
            case ENU.WFCode.BV:
                break;
            default:
                break;
        }
        return ProjectCode;
    }

    public bool ProgressWorkflow(string ProjectCode, ref ERR.UIException exUI)
    {
        return true;
        //changed
    }

    public DataTable GetWorkflowItemHistory(string ProjectCode)
    {
        return null;
    }

    public DataTable ListWorkflowsForClient(int ClientId)
    {
        return null;
    }

    public DataTable ListWorkflowsForCustomer(int CustomerId)
    {
        return null;
    }


    /// <summary>
    /// Starts RP Type Workflow for specified Client and Customer and returns Project Code (if ex occurs then this is null)
    /// </summary>
    /// <param name="ClientId"></param>
    /// <param name="CustomerId"></param>
    /// <returns></returns>
    private static string WFRPStart(int ClientId, int CustomerId, ref ERR.UIException exUI)
    {
        string ProjectCode=null;
        try
        {
            if (DAL.WFStart(ENU.WFCode.RP, ClientId, CustomerId, out ProjectCode))
            {
                if (ProjectCode==null)
                {
                    throw new ERR.UIException("Unable to start workflow");
                }
                if (ProjectCode=="EXISTS")
                {
                    throw new ERR.UIException("There is an existing incomplete RP workflow for this client. You can not create double RP Workflows.");
                }
            }

            return ProjectCode;
        }
        catch (Exception ex)
        {
            if (ex is ERR.UIException)
            {
                exUI = (ERR.UIException)ex;
                return ProjectCode;
            }
            else
            {
                throw ex;
            }

        }

    }

}